#!/bin/bash

HZRCB_JAR_HOME=$HOME/data/jenkins/workspace/hzrcb-parent/build
HZRCB_RUN_HOME=$HOME/data/hzrcb

mkdir -p $HZRCB_RUN_HOME

EMPTY_STR=""

CURRENT_DIR=""
CURRENT_DIR_FILE=$HZRCB_RUN_HOME/current.dir
if [ -f $CURRENT_DIR_FILE ]; then
    CURRENT_DIR=`cat $CURRENT_DIR_FILE`
fi

backupPreviewBuild() {
    DIR_NAME=$1
    DIR_NAME=${DIR_NAME/"$HZRCB_RUN_HOME/"/$EMPTY_STR}
    echo "backup preview build at $DIR_NAME"
    cd $HZRCB_RUN_HOME && \
        tar czf $DIR_NAME.tar.gz $DIR_NAME && \
        rm -Rf $DIR_NAME
}

beforeStart() { 
    stop

    if [ "$CURRENT_DIR" != "" ] && [ -d $CURRENT_DIR ]; then
        backupPreviewBuild $CURRENT_DIR &
    fi

    DATE=`date '+%Y%m%d_%H%M%S'`
    CURRENT_DIR=$HZRCB_RUN_HOME/$DATE
    echo "copy resources to $CURRENT_DIR"
    mkdir -p $CURRENT_DIR && \
        cp -rp $HZRCB_JAR_HOME/{*.jar,*.war,.nuxt,nuxt.config.js,package.json} $CURRENT_DIR && \
        ln -s $HZRCB_JAR_HOME/node_modules $CURRENT_DIR/node_modules && \
        echo "$CURRENT_DIR" > $CURRENT_DIR_FILE
}

startJar() {
    echo "start jars..."
    for JAR in $CURRENT_DIR/*.jar
    do
        if [[ "$JAR" != *"privilege-ui"* ]]; then
            NAME=$JAR
            NAME=${NAME/"$CURRENT_DIR/"/$EMPTY_STR}
            NAME=${NAME/\.jar/$EMPTY_STR}

            LOG_FILE=$CURRENT_DIR/$NAME.log
            PID_FILE=$CURRENT_DIR/$NAME.pid

            if [ -f $PID_FILE ]; then
                kill -9 `cat $PID_FILE`
                rm $PID_FILE
            fi

            nohup java -Xmx256M -jar $JAR >> $LOG_FILE 2>&1 &

            PID=$!
            echo $PID > $PID_FILE
            echo "  running $NAME at $PID"
        fi
    done
}

stopJar() {
    for PID_FILE in $CURRENT_DIR/*.pid
    do
        if [ -f $PID_FILE ]; then
            PID=`cat $PID_FILE`
            kill -9 $PID
            rm $PID_FILE

            NAME=$PID_FILE
            NAME=${NAME/"$CURRENT_DIR/"/$EMPTY_STR}
            NAME=${NAME/\.pid/$EMPTY_STR}
            echo "$NAME stopped"
        fi
    done
}

startPrivilege.old() {
    TOMCAT_WEBAPPS=$CURRENT_DIR/.tomcat_webapps
    PRIVILEGE_WAR=`ls $CURRENT_DIR/*privilege*.war`
    mkdir -p $TOMCAT_WEBAPPS
    # unzip the privilege war
    unzip -qo $PRIVILEGE_WAR -d $TOMCAT_WEBAPPS/ROOT
    # resolve some resources
    sed -i -e 's/port: 8000/port: 8080/' $TOMCAT_WEBAPPS/ROOT/WEB-INF/classes/application.yml
    mkdir -p $TOMCAT_WEBAPPS/ROOT/WEB-INF/ralasafe/xml/ralasafe/userType
    # remove jar and war
    rm $PRIVILEGE_WAR `ls $CURRENT_DIR/*privilege*.jar`

    CID_FILE=$CURRENT_DIR/api-privilege.cid
    docker run -d --rm \
        -p 8000:8080 \
        -v $TOMCAT_WEBAPPS:/usr/local/tomcat/webapps \
        -e eureka.client.service-url.defaultZone=http://172.17.0.1:8761/eureka/ \
        tomcat:8-jre8-alpine \
        > $CID_FILE
    CONTAINER_ID=`cat $CID_FILE`
    echo "running privilege at $CONTAINER_ID"
}

stopPrivilege.old() {
    CID_FILE=$CURRENT_DIR/api-privilege.cid
    if [ -f $CID_FILE ]; then
        CONTAINER_ID=`cat $CID_FILE`
        docker stop $CONTAINER_ID
        rm $CID_FILE
        echo "privilege stopped"
    fi
}

startPrivilege() {
    TOMCAT_VERSION=8.5.32
    TOMCAT_NAME=apache-tomcat-$TOMCAT_VERSION
    if [ ! -d $HZRCB_RUN_HOME/$TOMCAT_NAME ]; then
        WORK_DIR=$PWD
        cd $HZRCB_RUN_HOME
        wget http://mirrors.shu.edu.cn/apache/tomcat/tomcat-8/v$TOMCAT_VERSION/bin/$TOMCAT_NAME.tar.gz && \
            tar zxf $TOMCAT_NAME.tar.gz && \
            sed -i -e 's/"8080"/"8000"/g' $TOMCAT_NAME/conf/server.xml && \
            rm $TOMCAT_NAME.tar.gz
        cd $WORK_DIR
    fi


    PRIVILEGE_WAR=`ls $CURRENT_DIR/*privilege-ui*.war`

    rm -Rf $HZRCB_RUN_HOME/$TOMCAT_NAME/webapps
    mkdir $HZRCB_RUN_HOME/$TOMCAT_NAME/webapps
    ln -s $PRIVILEGE_WAR $HZRCB_RUN_HOME/$TOMCAT_NAME/webapps/ROOT.war
    nohup $HZRCB_RUN_HOME/$TOMCAT_NAME/bin/catalina.sh run >> $CURRENT_DIR/api-privilege-ui.log 2>&1 &

    PID=$!
    echo $PID > $CURRENT_DIR/api-privilege-ui.pid
    echo "running privilege ui at $PID"
}

stopPrivilege() {
    PID_FILE=$CURRENT_DIR/api-privilege-ui.pid
    if [ -f $PID_FILE ]; then
        PID=`cat $PID_FILE`
        kill -9 $PID
        rm $PID_FILE
        echo "privilege ui stopped"
    fi
}

startWeb() {
    source $HOME/.nvm/nvm.sh
    nvm use --delete-prefix node
    WORK_DIR=$PWD
    cd $CURRENT_DIR
    nohup ./node_modules/.bin/nuxt start >> web.log 2>&1 &
    PID=$!
    echo $PID > $CURRENT_DIR/web.pid
    echo "running web at $PID"
    cd $WORK_DIR
}

stopWeb() {
    PID_FILE=$CURRENT_DIR/web.pid
    if [ -f $PID_FILE ]; then
        PID=`cat $PID_FILE`
        kill -9 $PID
        rm $PID_FILE
        echo "web stopped"
    fi
}

start() {
    beforeStart
    startJar
    startPrivilege
    startWeb
}

stop() {
    stopJar
    stopPrivilege
    stopWeb
}


case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    startP)
        startPrivilege
        ;;
    startD)
        startJar
        startPrivilege
        startWeb
        ;;
    *)
        start
        ;;
esac
